package org.ala.clickhouse.xml.fields;

/**
 *	聚合函数
 *
 *	@author ala
 *	@date 2025年6月9日
 */
public abstract class Aggregate {

	
	/**
	 * 	列名
	 */
	protected String column;
	public Aggregate(String column) {this.column = column;}
	public String getColumn() {
		return column;
	}
	/**
	 * 	输出聚合
	 */
	public abstract String toSql();
	
	
	/**
	 * 	极大极小聚合
	 */
	public static class Max extends Aggregate {
		public Max(String column) { super(column); }
		public String toSql() {
			return String.format("MAX(%s) as %s", column, column);
		}
	}
	public static class Min extends Aggregate {
		public Min(String column) { super(column); }
		public String toSql() {
			return String.format("MIN(%s) as %s", column, column);
		}
	}
	/**
	 * 	求和
	 */
	public static class Sum extends Aggregate {
		public Sum(String column) { super(column); }
		public String toSql() {
			return String.format("SUM(%s) as %s", column, column);
		}
	}
	/**
	 * 	求平均
	 */
	public static class Avg extends Aggregate {
		public Avg(String column) { super(column); }
		public String toSql() {
			return String.format("AVG(%s) as %s", column, column);
		}
	}
	/**
	 * 	求数量
	 */
	public static class Count extends Aggregate {
		public Count(String column) { super(column); }
		public String toSql() {
			return String.format("COUNT(%s) as %s", column, column);
		}
	}
	/**
	 * 	去重数量
	 */
	public static class DistinctCount extends Aggregate {
		public DistinctCount(String column) { super(column); }
		public String toSql() {
			return String.format("COUNT(DISTINCT %s) as %s", column, column);
		}
	}
	/**
	 * 	方差
	 */
	public static class VarPop extends Aggregate {
		public VarPop(String column) { super(column); }
		public String toSql() {
			return String.format("varPop(%s) as %s", column, column);
		}
	}
	/**
	 * 	标准差
	 */
	public static class StddevPop extends Aggregate {
		public StddevPop(String column) { super(column); }
		public String toSql() {
			return String.format("stddevPop(%s) as %s", column, column);
		}
	}
	/**
	 * 	位与
	 */
	public static class BitAnd extends Aggregate {
		public BitAnd(String column) { super(column); }
		public String toSql() {
			return String.format("bitAnd(%s) as %s", column, column);
		}
	}
	/**
	 * 	位或
	 */
	public static class BitOr extends Aggregate {
		public BitOr(String column) { super(column); }
		public String toSql() {
			return String.format("bitOr(%s) as %s", column, column);
		}
	}
	/**
	 * 	位异或
	 */
	public static class BitXor extends Aggregate {
		public BitXor(String column) { super(column); }
		public String toSql() {
			return String.format("bitXor(%s) as %s", column, column);
		}
	}
	/**
	 * 	分位数
	 */
	public static class Quantile extends Aggregate {
		protected double p;
		public Quantile(double p, String column) { super(column); this.p = p; }
		public String toSql() {
			return String.format("quantile(%f)(%s) as %s", p, column);
		}
	}
	/**
	 * 	中位数
	 * 	<p>	50%采样的特殊中位数
	 */
	public static class Median extends Aggregate {
		public Median(String column) { super(column);}
		public String toSql() {
			return String.format("median(%s) as %s", column);
		}
	}
}
